package com.xctech.yace.swing.frame;

import com.xctech.yace.util.CheckUtil;
import com.xctech.yace.util.DirUtil;
import com.xctech.yace.util.RSAEncryptUtil;
import lombok.SneakyThrows;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

public class LockFrame extends JDialog {

    private JPasswordField passwordField;
    private JTextField nameField;
    private JLabel label_3;

    private static Properties props = new Properties();

    static {
        String filePath = DirUtil.getOrgPath()+"conf/password.properties";
        try {
            props.load(new FileInputStream(filePath));
        } catch (
                IOException e) {
            e.printStackTrace();
        }
     }

    public LockFrame(JFrame jframe, String str, boolean bool) {
        super(jframe, str, true);
        setSize(300, 150);
        //这个方法尤为重要，只要屏蔽了右上角的关闭按钮
        setUndecorated(true);
        //设置总是将dialog显示在屏幕的最前面
        setAlwaysOnTop(true);
        setLocation(
                (Toolkit.getDefaultToolkit().getScreenSize().width - getWidth()) / 2,
                (Toolkit.getDefaultToolkit().getScreenSize().height - getHeight()) / 2);
        setPreferredSize(new Dimension(200, 130));
        getContentPane().setLayout(new BorderLayout(0, 0));

        JPanel rootPanel = new JPanel();
        rootPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(0,
                255, 255), null));
        getContentPane().add(rootPanel, BorderLayout.CENTER);
        rootPanel.setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(10, 60));
        rootPanel.add(panel, BorderLayout.NORTH);

        //panel.setLayout(null);
        JLabel label = new JLabel("登录");
        label.setFont(new Font("宋体", Font.PLAIN, 18));
        panel.add(label, "cell 1 0");
        label_3 = new JLabel("");
        label_3.setBackground(Color.RED);
        panel.add(label_3,"cell 1 1");

        JPanel panel_1 = new JPanel();
        panel_1.setPreferredSize(new Dimension(10, 5));
        rootPanel.add(panel_1, BorderLayout.CENTER);
        panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        JSeparator separator = new JSeparator();
        separator.setForeground(new Color(74, 219, 219));
        separator.setPreferredSize(new Dimension(280, 2));
        panel_1.add(separator);

        JPanel panel_2 = new JPanel();
        panel_2.setPreferredSize(new Dimension(10, 60));
        rootPanel.add(panel_2, BorderLayout.SOUTH);
        panel_2.setLayout(null);

        JLabel label_4 = new JLabel("用户");
        label_4.setBounds(26, 0, 36, 15);
        panel_2.add(label_4);
        nameField = new JTextField();
        nameField.setBounds(66, 0, 210, 21);
        panel_2.add(nameField);
        nameField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
//                System.out.println(e.getKeyCode());
                label_3.setText("");
            }
        });

        JLabel label_2 = new JLabel("密码");
        label_2.setBounds(26, 25, 36, 15);
        panel_2.add(label_2);
        passwordField = new JPasswordField();
        passwordField.setBounds(66, 25, 210, 21);
        passwordField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent event) {
//                System.out.println(event.getKeyCode());
                if (event.getKeyCode() == 10) {
                    String pw = passwordField.getText().trim();
                    String name = nameField.getText().trim();
                    //获取登录的用户密码
                    if (CheckUtil.check(name,pw)) {
                        dispose();
                    } if(pw.equals(props.getProperty("exit"))){
                        System.exit(-1);
                    }else {
                        label_3.setText("对不起，请您输入正确的密码！");
                        passwordField.setText("");
                        return;
                    }
                }
            }
        });
        passwordField.setPreferredSize(new Dimension(210, 21));
        panel_2.add(passwordField);
    }


}
